# To change this template, choose Tools | Templates
# and open the template in the editor.
require 'rubygems'
require 'net/toc'

$list = {}
begin
  Net::TOC.new("IEDLocalList", "tennis") do |msg, buddy|
    #while true
    #  puts "What is the command directory?"
    #  input1 = gets.chomp
    #   File::open(input1, 'r') do |commands|
    #    command = commands.sysread(1)
    #    puts "Command = " + command + "\n"
    msg.gsub!(/<\/?[^>]*>/, "")
    puts msg
    command = msg.split('')[0]
    if(command.match(/[0-9]/))
      command = 'a'
      msg = " " + msg
    else
      msg [0] = ''
    end

    puts command
    if(command == "a")
      msg.downcase!
      msg += ' '
      while(msg.match(/s\s/))
        msg[/s\s/] = ' '
      end

      while(msg.match(/\d+\s/))
        number = msg[/\d+\s/]
        number[/\s/] = '' if (number.match(/\s/))
        msg[/\d+\s/] = ''
        item = msg[/\w+\s/]
        #item[/_/] = " " while item.match(/_/)
        item[/\s/] = '' if (item.match(/\s/))
        msg[/\w+\s/] = ''
        $list[item] = $list.fetch(item,0) + number.to_i
      end
      buddy.send_im("Addition to the list successful")
    end
    if(command == "p")
      output_list = "Grocery List:"
      $list.sort{|a,b| b[1]<=>a[1]}.each { |elem|
        elem[0] += "s" if(elem[1] > 1)
        output_list += "\n#{elem[1]} #{elem[0]}"
      }
      buddy.send_im(output_list)
    end
    if(command == "c")
      $list={}
      buddy.send_im("List has been cleared")
    end
    if(command == "s")
    
    end
    if(command == "r")
      msg.downcase!
      msg += ' '
      while(msg.match(/s\s/))
        msg[/s\s/] = ' '
      end
      while(msg.match(/\d+\s/))
        number = msg[/\d+\s/] if msg.match(/\d+\s/)
        number[/\s/] = '' if ((not number == nil) and number.match(/\s/))
        msg[/\d+\s/] = '' if not number == nil
        item = msg[/\w+\s/]
        item[/\s/] = '' if (item.match(/\s/))
        msg[/\w+\s/] = ''
        error_msg = ""
        if number == nil
          $list.delete(item) if $list.include?(item)
        else
          if($list.include?(item))
            if($list[item] >= number.to_i)
              $list[item] -= number.to_i
            else
              error_msg += "\nThere was only " + ($list[item]).to_s + " " + item + " in the list"
              $list.delete(item)
            end
          else
            error_msg += "\nNo " + item + " in list"
          end
          if error_msg == ""
            buddy.send_im("Sucessfully removed items")
          else
            error_msg = "Remove Sucessful with errors:" + error_msg
            buddy.send_im(error_msg)
          end
        end
      end
    end
    if(command == "h")
      help_message = "\nWelcome to LocalList!  Local List makes keeping a grocery list easy!  There are 4 commands:\n\n1)a add to list.  This is the default behavior.  Usage: 'a 1 can_of_gravy' is the same as '1 can_of_gravy'  Please note that '1 can of gravy' will just add '1 can' to the list.\n\n2)p print the list.  This shows the current list. Usage: 'p'\n\n3)r remove from list. Usage: 'r 1 can_of_gravy'\n\n4)c clears the list.  Useage: 'c'\n\nThank you for using LocalList!  Enjoy!"
      buddy.send_im(help_message)

    end
    if(command[/[arpch]/] == nil)
      buddy.send_im("Unknown Command: " + command)
    end
  end

rescue
  redo
end
